home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / vopl / glvopl.lha / glvopl / src / plot.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-08  |  2.8 KB  |  150 lines

  1. #ifdef SGI_GL
  2. #include "gl.h"
  3. #else
  4. #include "vogl.h"
  5. #endif
  6.  
  7. #include "vopl.h"
  8.  
  9. extern    double log10();
  10.  
  11. /*
  12.  * plot2
  13.  *
  14.  *    display the 2-d graph described by x and y;
  15.  */
  16. plot2(x, y, n)
  17.     float    x[], y[];
  18.     int    n;
  19. {
  20.     int    i;
  21.     char    err_buf[EBUF_SIZE];
  22.     float    tx, ty;
  23.     Screencoord    left, right, bottom, top;
  24.     float    a, b, c, d;
  25.  
  26.     /*
  27.      * Set the view up so that clipping happens...
  28.      */
  29.  
  30.     pushmatrix();
  31.     pushviewport();
  32.     pushattributes();
  33.  
  34.     getviewport(&left, &right, &bottom, &top);
  35.  
  36.     c = (top + bottom) * 0.5;
  37.     a = (top - c);
  38.  
  39.     /* Don't forget fudge factor... in X */
  40.     b = (right - left) / (2 + FUDGE);
  41.     d = left + b;
  42.  
  43.     viewport((short)(b * XMIN + d), (short)(b * XMAX + d),
  44.          (short)(a * YMIN + c), (short)(a * YMAX + c)
  45.     );
  46.  
  47.     if (!plotdev.axes[XIND].scaleset)
  48.         adjustscale(x, n, 'x');
  49.  
  50.     if (!plotdev.axes[YIND].scaleset)
  51.         adjustscale(y, n, 'y');
  52.  
  53.         ortho2(WhatX(plotdev.axes[XIND].min), 
  54.            WhatX(plotdev.axes[XIND].max),
  55.            WhatY(plotdev.axes[YIND].min),
  56.            WhatY(plotdev.axes[YIND].max));
  57.  
  58.     /*
  59.      * Do whatever type of fit is required....
  60.      */
  61.     switch (plotdev.fit) {
  62.     case NO_LINES:
  63.         /*
  64.           * Don't do a sniveling thing......
  65.          */
  66.         break;
  67.  
  68.     case STRAIGHT_LINE:
  69.         /*
  70.          * Just draw it.....
  71.          */
  72.  
  73.         move2(WhatX(x[0]), WhatY(y[0]));
  74.         for (i = 1; i < n; i++)
  75.             draw2(WhatX(x[i]), WhatY(y[i]));
  76.  
  77.         break;
  78.     case LEAST_SQUARE:
  79.         /*
  80.           * Do an orthogonal polynomial fit.....
  81.          */
  82.  
  83.         /*
  84.          * If degree is zero then simply plot a line y = average y
  85.          */
  86.         if (plotdev.degree == 0)
  87.             avefit(x, y, n);
  88.         /*
  89.          * If degree is 1 then it's a simple linear least square fit...
  90.          */
  91.         else if (plotdev.degree == 1) 
  92.             llsfit(x, y, n);
  93.         else 
  94.             orthofit(x, y, plotdev.degree, n);
  95.         break;
  96.     case CUBIC_SPLINE:
  97.         /*
  98.          * Do cubic spline fits......
  99.          */
  100.         /* 
  101.          * If no endslopes have been used then use VOGLE curve
  102.          * routine with cardinal splines.
  103.          */
  104.         if (plotdev.splinetype == FREE) {
  105.             defbasis((short)1, cardinal);
  106.             curvebasis((short)1);
  107.             curveprecision(20);
  108.             cubicsp(x, y, n);
  109.         } else
  110.             spline(x, y, n);
  111.         break;
  112.     case POWER_EQN:
  113.         /*
  114.          * do power equation fit
  115.          */
  116.         pefit(x, y, n);
  117.         break;
  118.     case SGR_FIT:
  119.         /*
  120.          * do a saturated growth rate fit
  121.          */
  122.         sgrfit(x, y, n);
  123.         break;
  124.     default:
  125.         sprintf(err_buf, "plot2: unknown fit type %d", plotdev.fit);
  126.         vopl_error(err_buf);
  127.     }
  128.  
  129.     /* 
  130.      * Set up the marker size
  131.      */
  132.  
  133.     if (plotdev.markerspacing && plotdev.marker != (char *)NULL) {
  134.         hcentertext(1);
  135.         tx = plotdev.markerscale * TEXTWIDTH * WhatX((plotdev.axes[XIND].max - plotdev.axes[XIND].min));
  136.         ty = plotdev.markerscale * TEXTHEIGHT * WhatY((plotdev.axes[YIND].max - plotdev.axes[YIND].min));
  137.  
  138.         htextsize(tx, ty);
  139.  
  140.         for (i = 0; i < n; i += plotdev.markerspacing) {
  141.             move2(WhatX(x[i]), WhatY(y[i]) + 0.15 * ty);
  142.             hcharstr(plotdev.marker);
  143.         }
  144.     }
  145.  
  146.     popattributes();
  147.     popviewport();
  148.     popmatrix();
  149. }
  150.